<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/6/8
 * Time: 10:15
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\business;

use app\common\model\SystemAdmin;
use app\common\validate\User;
use Htlove\Jwt;
use think\Exception;

class BusUser extends BusBase
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new SystemAdmin();
    }

    public function doLogin(array $data = [])
    {

        $validate = new User();
        if (!$validate->scene('login')->check($data)) {
            throw new Exception($validate->getError());
        }
        //查询用户
        try {
            $user = $this->model->getUserMobileByInfo($data['mobile']);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        if (!$user) {
            throw new Exception("用户不存在");
        }
        $userInfo = $user->toArray();
        if ($userInfo['status'] != 1) {
            throw new Exception("用户已经被禁用");
        }

        //查询密码
        $password = password($data['password']);
        if ($password != $userInfo['password']) {
            throw new Exception("用户名和密码不匹配");
        }
        $res_data = [
            "id" => $userInfo['id'],
            "username" => $userInfo['username'],
            "mobile" => $userInfo['phone'],
            "create_time" => $userInfo['create_time'],
            "logo"=>$userInfo['head_img']
        ];

        $token = Jwt::getInstance()->getToken($res_data);
        $res_data['token'] = $token;
        return $res_data;
    }
}